<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <title>Memcached  &middot; sitename</title>
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="HandheldFriendly" content="True">
<meta name="MobileOptimized" content="320">
<meta name="viewport" content="width=device-width, initial-scale=1">


<meta name="description" content="" />

<meta name="keywords" content="one, two, ">


<meta property="og:title" content="Memcached  &middot; sitename ">
<meta property="og:site_name" content="sitename"/>
<meta property="og:url" content="https://lvzongcheng.gitee.io/framework/memcached/" />
<meta property="og:locale" content="en-EN">


<meta property="og:type" content="article" />
<meta property="og:description" content=""/>
<meta property="og:article:published_time" content="2017-12-14T00:00:00Z" />
<meta property="og:article:modified_time" content="2017-12-14T00:00:00Z" />

  
    
<meta property="og:article:tag" content="one">
    
<meta property="og:article:tag" content="two">
    
  

  

<script type="application/ld+json">
  {
    "@context": "http://schema.org",
    "@type": "Article",
    "headline": "Memcached",
    "author": {
      "@type": "Person",
      "name": "LvZongCheng"
    },
    "datePublished": "2017-12-14",
    "description": "",
    "wordCount":  356 
  }
</script>



<link rel="canonical" href="https://lvzongcheng.gitee.io/framework/memcached/" />

<link rel="apple-touch-icon-precomposed" sizes="144x144" href="https://lvzongcheng.gitee.io/touch-icon-144-precomposed.png">
<link href="https://lvzongcheng.gitee.io/favicon.png" rel="icon">

<meta name="generator" content="Hugo 0.60.0" />

  <!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->

<link href='https://fonts.googleapis.com/css?family=Merriweather:300%7CRaleway%7COpen+Sans' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="/css/font-awesome.min.css">
<link rel="stylesheet" href="/css/style.css">
<link rel="stylesheet" href="/css/highlight/default.css">

  
  
	<script>
	  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
	  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
	  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
	  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

	  ga('create', 'Your Google Analytics tracking code', 'auto');
	  ga('send', 'pageview');

	</script>

</head>
<body>
  <main id="main-wrapper" class="container main_wrapper has-sidebar">
    <header id="main-header" class="container main_header">
  <div class="container brand">
  <div class="container title h1-like">
  <a class="baselink" href="https://lvzongcheng.gitee.io">
  LvZongCheng

</a>

</div>

  
<div class="container topline">
  
  淡定 执著 进取 自律


</div>


</div>

  <nav class="container nav primary no-print">
  

<a class="homelink" href="https://lvzongcheng.gitee.io">home</a>


  
<a href="https://lvzongcheng.gitee.io/framework" title="Show list of Frameworks">Frameworks</a>

<a href="https://lvzongcheng.gitee.io/java" title="Show list of Java">Java</a>

<a href="https://lvzongcheng.gitee.io/linux" title="Show list of Linux">Linux</a>

<a href="https://lvzongcheng.gitee.io/redis" title="Show list of Redis">Redis</a>

<a href="https://lvzongcheng.gitee.io/sql" title="Show list of SQL">SQL</a>

<a href="https://lvzongcheng.gitee.io/Spring" title="Show list of Spring">Spring</a>

<a href="https://lvzongcheng.gitee.io/tools" title="Show list of Tools">Tools</a>

<a href="https://lvzongcheng.gitee.io/webpage" title="Show list of WebPage">WebPage</a>

<a href="https://lvzongcheng.gitee.io/information" title="Show list of information">information</a>

<a href="https://lvzongcheng.gitee.io/network" title="Show list of network">network</a>

<a href="https://lvzongcheng.gitee.io/%E5%BE%AE%E6%9C%8D%E5%8A%A1" title="Show list of 微服务">微服务</a>

<a href="https://lvzongcheng.gitee.io/%E6%80%BB%E7%BB%93" title="Show list of 总结">总结</a>

<a href="https://lvzongcheng.gitee.io/%E6%9C%BA%E5%99%A8%E8%87%AA%E5%8A%A8%E5%8C%96" title="Show list of 机器自动化">机器自动化</a>

<a href="https://lvzongcheng.gitee.io/%E6%B1%87%E7%BC%96" title="Show list of 汇编">汇编</a>

<a href="https://lvzongcheng.gitee.io/%E8%AE%B0%E4%BA%8B%E6%9C%AC" title="Show list of 记事本">记事本</a>


</nav>

<div class="container nav secondary no-print">
  
<a id="contact-link-email" class="contact_link" rel="me" aria-label="Email" href="mailto:Lyc19880405@163.com">
  <span class="fa fa-envelope-square"></span></a>



<a id="contact-link-github" class="contact_link" rel="me" aria-label="Github" href="https://github.com/enten/hugo-boilerplate">
  <span class="fa fa-github-square"></span></a>




 


















</div>


  

</header>


<article id="main-content" class="container main_content single">
  <header class="container hat">
  <h1>Memcached
</h1>

</header>

  <div class="container content">
  <h4 id="java-memcahced">Java项目添加 memcahced</h4>
<p>java memcached client没有官方的maven repository可供使用，因此使用时需要手动将其安装到本地repository
<a href="https://github.com/gwhalin/Memcached-Java-Client/downloads">java memcached client的jar包下载地址</a>
目前2.6.6版本的java memcached client要依赖slf4j-simple、slf4j-api、commons-pool三个包，依此我们可以为其编辑一个pom文件，
将java memcached client的jar包和pom文件一起安装，解决java memcached client对其他jar包的依赖。安装命令：</p>
<p>mvn install:install-file -Dfile=F:\comm\youboy\java_memcached-release_2.6.6\java_memcached-release_2.6.6.jar -DpomFile=F:\comm\youboy\java_memcached-release_2.6.6\pom.xml</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-xml" data-lang="xml"><span style="color:#75715e">&lt;?xml version=&#34;1.0&#34; encoding=&#34;UTF-8&#34;?&gt;</span>    
<span style="color:#f92672">&lt;project</span> <span style="color:#a6e22e">xsi:schemaLocation=</span><span style="color:#e6db74">&#34;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&#34;</span> <span style="color:#a6e22e">xmlns=</span><span style="color:#e6db74">&#34;http://maven.apache.org/POM/4.0.0&#34;</span>    
  <span style="color:#a6e22e">xmlns:xsi=</span><span style="color:#e6db74">&#34;http://www.w3.org/2001/XMLSchema-instance&#34;</span><span style="color:#f92672">&gt;</span>    
 <span style="color:#f92672">&lt;modelVersion</span><span style="color:#f92672">&gt;</span>4.0.0<span style="color:#f92672">&lt;/modelVersion&gt;</span>  
  <span style="color:#f92672">&lt;groupId</span><span style="color:#f92672">&gt;</span>com.lyz<span style="color:#f92672">&lt;/groupId&gt;</span>  
  <span style="color:#f92672">&lt;artifactId</span><span style="color:#f92672">&gt;</span>lyz<span style="color:#f92672">&lt;/artifactId&gt;</span>  
  <span style="color:#f92672">&lt;version</span><span style="color:#f92672">&gt;</span>1.0.0-SNAPSHOT<span style="color:#f92672">&lt;/version&gt;</span>  
  <span style="color:#f92672">&lt;packaging</span><span style="color:#f92672">&gt;</span>war<span style="color:#f92672">&lt;/packaging&gt;</span>    
<span style="color:#f92672">&lt;dependencies</span><span style="color:#f92672">&gt;</span>    
   <span style="color:#f92672">&lt;dependency</span><span style="color:#f92672">&gt;</span>    
       <span style="color:#f92672">&lt;groupId</span><span style="color:#f92672">&gt;</span>com.danga<span style="color:#f92672">&lt;/groupId&gt;</span>    
      <span style="color:#f92672">&lt;artifactId</span><span style="color:#f92672">&gt;</span>java-memcached<span style="color:#f92672">&lt;/artifactId&gt;</span>    
      <span style="color:#f92672">&lt;version</span><span style="color:#f92672">&gt;</span>2.6.6<span style="color:#f92672">&lt;/version&gt;</span>   
  <span style="color:#f92672">&lt;/dependency&gt;</span>   
  <span style="color:#f92672">&lt;dependency</span><span style="color:#f92672">&gt;</span>    
    <span style="color:#f92672">&lt;groupId</span><span style="color:#f92672">&gt;</span>org.slf4j<span style="color:#f92672">&lt;/groupId&gt;</span>    
    <span style="color:#f92672">&lt;artifactId</span><span style="color:#f92672">&gt;</span>slf4j-simple<span style="color:#f92672">&lt;/artifactId&gt;</span>    
    <span style="color:#f92672">&lt;version</span><span style="color:#f92672">&gt;</span>1.6.1<span style="color:#f92672">&lt;/version&gt;</span>    
  <span style="color:#f92672">&lt;/dependency&gt;</span>    
  <span style="color:#f92672">&lt;dependency</span><span style="color:#f92672">&gt;</span>    
    <span style="color:#f92672">&lt;groupId</span><span style="color:#f92672">&gt;</span>org.slf4j<span style="color:#f92672">&lt;/groupId&gt;</span>    
    <span style="color:#f92672">&lt;artifactId</span><span style="color:#f92672">&gt;</span>slf4j-api<span style="color:#f92672">&lt;/artifactId&gt;</span>    
    <span style="color:#f92672">&lt;version</span><span style="color:#f92672">&gt;</span>1.6.1<span style="color:#f92672">&lt;/version&gt;</span>    
  <span style="color:#f92672">&lt;/dependency&gt;</span>    
  <span style="color:#f92672">&lt;dependency</span><span style="color:#f92672">&gt;</span>    
    <span style="color:#f92672">&lt;groupId</span><span style="color:#f92672">&gt;</span>commons-pool<span style="color:#f92672">&lt;/groupId&gt;</span>    
    <span style="color:#f92672">&lt;artifactId</span><span style="color:#f92672">&gt;</span>commons-pool<span style="color:#f92672">&lt;/artifactId&gt;</span>    
    <span style="color:#f92672">&lt;version</span><span style="color:#f92672">&gt;</span>1.5.6<span style="color:#f92672">&lt;/version&gt;</span>    
  <span style="color:#f92672">&lt;/dependency&gt;</span>    
<span style="color:#f92672">&lt;/dependencies&gt;</span>    
<span style="color:#f92672">&lt;/project&gt;</span> 
</code></pre></div><h4 id="memcached-">Memcached 安装</h4>
<h6 id="--libevent">一、  检查libevent</h6>
<p>首先检查系统中是否安装了libevent</p>
<pre><code>rpm -qa|grep libevent  
</code></pre>
<p>如果安装了则查看libevent的安装路径，后续安装时需要用到</p>
<pre><code>rpm -ql libevent  
</code></pre>
<p>如果没有安装，则先安装libevent</p>
<h6 id="libevent">二、安装libevent：</h6>
<p>1.首先下载libevent安装包</p>
<pre><code>wget http://www.monkey.org/~provos/libevent-1.4.12-stable.tar.gz     
</code></pre>
<p>2.解压缩安装包</p>
<pre><code>tar zxvf libevent-1.4.12-stable.tar.gz -C /usr/local/  
</code></pre>
<p>3.进入解压后的目录</p>
<pre><code>cd libevent-1.4.12-stable/  
</code></pre>
<p>4.配置编译、安装</p>
<pre><code>./configure -prefix=/usr/libevent  
make  
make install  
</code></pre>
<p>安装完libevent之后为了让动态链接库为系统所共享，需要执行以下动态链接库的管理命令ldconfig</p>
<p>不过在执行ldconfig命令之前需要注意以下libevent的安装目录</p>
<h6 id="libeventmemcached">三、安装完libevent后正式开始安装memcached</h6>
<ol>
<li>
<p>首先是下载memcached的安装包</p>
<p>wget <a href="http://memcached.googlecode.com/files/memcached-1.4.15.tar.gz">http://memcached.googlecode.com/files/memcached-1.4.15.tar.gz</a></p>
</li>
<li>
<p>修改安装包的执行操作权限</p>
<p>chmod 777 memcached-1.4.15.tar.gz</p>
</li>
<li>
<p>解压安装包到指定目录</p>
<p>tar zxvf memcached-1.4.15.tar.gz -C /usr/local</p>
</li>
<li>
<p>进入解压后的目录</p>
<p>cd /usr/local/memcached-1.4.15/</p>
</li>
<li>
<p>配置、编译、安装</p>
</li>
</ol>
<p>注：configure时需要指定libevent的安装路径</p>
<pre><code>./configure -with-libevent=/usr/libevent/ -prefix=/usr/local/memcached  
make  
make install  
</code></pre>
<ol start="6">
<li>
<p>安装成功后启动一下看安装是否成功</p>
<p>/usr/local/memcached/bin/memcached -d -m 10m -p 11211 -u root</p>
</li>
</ol>
<p>启动参数介绍如下：和上面的命令不对应</p>
<p>-d选项是启动一个守护进程，</p>
<p>-m是分配给Memcache使用的内存数量，单位是MB，这里是10MB，</p>
<p>-u是运行Memcache的用户，这里是root，</p>
<p>-l是监听的服务器IP地址，如果有多个地址的话，这里指定了服务器的IP地址192.168.0.200，</p>
<p>-p是设置Memcache监听的端口，这里设置了12000，最好是1024以上的端口，</p>
<p>-c选项是最大运行的并发连接数，默认是1024，这里设置了256，按照服务器的负载量来设定，</p>
<p>-P是设置保存Memcache的pid文件，我这里是保存在 /tmp/memcached.pid，也可以启动多个守护进程，不过端口不能重复。</p>
<h6 id="memcachedshell">memcached启动的shell脚本</h6>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell"><span style="color:#75715e">#!/bin/sh  
</span><span style="color:#75715e"></span>echo <span style="color:#e6db74">&#34;</span><span style="color:#e6db74">Start to start memcached server </span><span style="color:#66d9ef">$(</span>date<span style="color:#66d9ef">)</span><span style="color:#e6db74">...</span><span style="color:#e6db74">&#34;</span>  
  
MEMCACHED<span style="color:#f92672">=</span>/usr/local/memcached/bin/memcached  
  
usage<span style="color:#f92672">(</span><span style="color:#f92672">)</span>  
<span style="color:#f92672">{</span>  
     echo <span style="color:#e6db74">&#34;</span><span style="color:#e6db74">usage: `basename </span>$0<span style="color:#e6db74">` port</span><span style="color:#e6db74">&#34;</span>  
<span style="color:#f92672">}</span>  
  
<span style="color:#66d9ef">if</span> <span style="color:#f92672">[</span> -n <span style="color:#e6db74">&#34;</span>$1<span style="color:#e6db74">&#34;</span> <span style="color:#f92672">]</span>  
<span style="color:#66d9ef">then</span>  
<span style="color:#f92672">{</span>  
     pid<span style="color:#f92672">=</span><span style="color:#e6db74">`</span>ps aux|grep memcached|grep <span style="color:#e6db74">&#34;</span>$1<span style="color:#e6db74">&#34;</span> |grep -v grep|awk <span style="color:#e6db74">&#39;{print $2}&#39;</span><span style="color:#e6db74">`</span>  
     <span style="color:#66d9ef">if</span> <span style="color:#f92672">[</span> -n <span style="color:#e6db74">&#34;</span>$pid<span style="color:#e6db74">&#34;</span> <span style="color:#f92672">]</span>  
     <span style="color:#66d9ef">then</span>  
     <span style="color:#f92672">{</span>  
          sleep <span style="color:#ae81ff">2</span>  
          echo <span style="color:#e6db74">&#34;</span><span style="color:#e6db74">kill memcached whic
</span></code></pre></div><p>把该脚本命名为startup.sh<br>
执行：./startup.sh 11211<br>
其中11211为memcached的启动端口，也可以通过自定义的端口来启动</p>
<p><code>查看是否启动：ps aux|grep memcached 查看版本 ：    cd /etc/init.d/ memcached -h   或者： [nihaoya@SHANGH-39-DX-APP ~]# telnet 127.0.0.1 11211</code></p>
<h6 id="memcached">memcached上配置日志</h6>
<p>memcache在linux上安装时并不支持显示地配置服务日志，
我们如果想要把memcache服务日志保存到日志文件中，则需要在启动参数中进行配置。
安装好memcache后，我们可以通过-h命令查看memcached支持的参数：
<strong>Shell代码</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-Shell" data-lang="Shell"><span style="color:#f92672">[</span>chenzhou@localhost ~<span style="color:#f92672">]</span>$ /usr/local/memcached/bin/memcached -h  
    <span style="color:#75715e">#其余的省略</span>  
    -v            verbose <span style="color:#f92672">(</span>print errors/warnings <span style="color:#66d9ef">while</span> in event loop<span style="color:#f92672">)</span>  
    -vv           very verbose <span style="color:#f92672">(</span>also print client commands/reponses<span style="color:#f92672">)</span>  
    -vvv          extremely verbose <span style="color:#f92672">(</span>also print internal state transitions<span style="color:#f92672">)</span>
</code></pre></div><p>从上面可以看到，启动memcached时有3个参数是和日志信息相关的：</p>
<p>其中-v代表打印普通的错误或者警告类型的日志信息</p>
<p>-vv比-v打印的日志更详细，包含了客户端命令和server端的响应信息</p>
<p>-vvv则是最详尽的，甚至包含了内部的状态信息打印</p>
<p>你可以根据你的实际需要来选择对应的参数，我这里使用-vv就OK了。</p>
<p>由于我们需要把日志信息保存在文件中，而不是在控制台输出，
而-vv等参数只能把日志信息输出在控制台。所以我们需要对-vv参数的输出进行数据流重定向，
关于重定向的知识在这里就不细述了，有兴趣的可以查下资料了解一下。</p>
<h6 id="memcached1">综上，启动memcached的命令如下：</h6>
<p><strong>Shell代码</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-Shell" data-lang="Shell">    /usr/local/memcached/bin/memcached -d -m <span style="color:#ae81ff">2048</span> -p $1 -u root -vv &gt;&gt; /tmp/memcached.log 2&gt;&amp;<span style="color:#ae81ff">1</span>  
</code></pre></div><p>重点在最后的：-vv &raquo; /tmp/memcached.log 2&gt;&amp;1</p>
<p>-vv &raquo; /tmp/memcached.log：代表把-vv的输出重定向到/tmp/memcached.log 文件中</p>
<p>2&gt;&amp;1的意思是把错误日志也一起写入到该文件中</p>
<p>启动成功后我们可以测试一下，首先起两个terminal，terminal1用来查看日志信息，terminal2进行client操作</p>
<p>terminal1：启动memcached后默认的日志信息如下<br>
<strong>Shell代码</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell"><span style="color:#f92672">[</span>chenzhou@localhost ~<span style="color:#f92672">]</span>$ tail -f /tmp/memcached.log   
&lt;<span style="color:#ae81ff">31</span> send buffer was 110592, now <span style="color:#ae81ff">268435456</span>  
&lt;<span style="color:#ae81ff">30</span> server listening <span style="color:#f92672">(</span>udp<span style="color:#f92672">)</span>  
&lt;<span style="color:#ae81ff">31</span> server listening <span style="color:#f92672">(</span>udp<span style="color:#f92672">)</span>  
&lt;<span style="color:#ae81ff">30</span> server listening <span style="color:#f92672">(</span>udp<span style="color:#f92672">)</span>  
&lt;<span style="color:#ae81ff">31</span> server listening <span style="color:#f92672">(</span>udp<span style="color:#f92672">)</span>  
&lt;<span style="color:#ae81ff">30</span> server listening <span style="color:#f92672">(</span>udp<span style="color:#f92672">)</span>  
&lt;<span style="color:#ae81ff">30</span> server listening <span style="color:#f92672">(</span>udp<span style="color:#f92672">)</span>  
&lt;<span style="color:#ae81ff">31</span> server listening <span style="color:#f92672">(</span>udp<span style="color:#f92672">)</span>  
&lt;<span style="color:#ae81ff">31</span> server listening <span style="color:#f92672">(</span>udp<span style="color:#f92672">)</span>  
&lt;<span style="color:#ae81ff">32</span> new auto-negotiating client connection
</code></pre></div><p>terminal2：往memcached里存入一个数据<br>
<strong>Shell代码</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">    <span style="color:#f92672">[</span>root@localhost bin<span style="color:#f92672">]</span><span style="color:#75715e"># telnet localhost 11211</span>  
    Trying 127.0.0.1...  
    Connected to localhost.localdomain <span style="color:#f92672">(</span>127.0.0.1<span style="color:#f92672">)</span>.  
    Escape character is <span style="color:#e6db74">&#39;^]&#39;</span>.  
    set name <span style="color:#ae81ff">0</span> <span style="color:#ae81ff">60</span> <span style="color:#ae81ff">5</span> chenzhou  
</code></pre></div><p>如上所示：使用set命令存入key为name value为chenzhou</p>
<p>terminal1日志记录：<br>
<strong>Shell代码</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">32: Client using the ascii protocol  
&lt;<span style="color:#ae81ff">32</span> set name <span style="color:#ae81ff">0</span> <span style="color:#ae81ff">60</span> <span style="color:#ae81ff">5</span> chenzhou 
</code></pre></div>
</div>


  
</article>
      <footer id="main-footer" class="container main_footer">
  

  <div class="container nav foot no-print">
  
<a href="https://lvzongcheng.gitee.io/license">license</a>


  <a class="toplink" href="#">back to top</a>

</div>

  <div class="container credits">
  
<div class="container footline">
  
  code with <!-- raw HTML omitted --><!-- raw HTML omitted -->


</div>


  
<div class="container copyright">
  
  (c) 2015 yourname.


</div>


</div>

</footer>

    </main>
    
<script type="text/javascript">
  (function() {
    
    
    if (window.location.hostname == "localhost")
      return;
    var dsq = document.createElement('script'); dsq.async = true; dsq.type = 'text/javascript';
    dsq.src = '//your_disqus_shortname.disqus.com/count.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  })();
</script>



<script src="/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>



    
  </body>
</html>

